/*
 * Tema 2 ASC
 * 2020 Spring
 */
#include "utils.h"

/*
 * Add your unoptimized implementation here
 */


double* my_solver(int N, double *A, double *B)
{
	int i, j, k;
	double *BAt, *AB, *C;
	double sum;

	BAt = (double *) malloc(N * N * sizeof(double));
	AB  = (double *) malloc(N * N * sizeof(double));
	C   = (double *) malloc(N * N * sizeof(double));

	for (i = 0; i < N; i ++) {
		for (j = 0; j < N; j++) {
			sum = 0.0;

			for (k = j; k < N; k++)
				sum += A[(j * N) + k] * B[(i * N) + k];

			BAt[(i * N) + j] = sum;

			sum = 0.0;

			for (k = i; k < N; k++)
				sum += A[(i * N) + k] * B[(k * N) + j];

			AB[(i * N) + j] = sum;
		}
	}

	for (i = 0; i < N; i++) {
		for (j = 0; j < N; j++) {
			sum = 0.0;

			for (k = i; k < N; k++)
				sum += A[(i * N) + k] * AB[(k * N) + j];

			C[(i * N) + j] = BAt[(i * N) + j] + sum;
		}
	}

	free(BAt);
	free(AB);

	return C;
}
